package com.example.administrator.newfragmentation.fragment.wx.first;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import com.example.administrator.newfragmentation.R;
import com.example.administrator.newfragmentation.adpter.wx.MsgAdapter;
import com.example.administrator.newfragmentation.entity.Chat;
import com.example.administrator.newfragmentation.entity.Msg;
import com.example.administrator.newfragmentation.fragmentMain.BaseFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;


/**
 * Created by YoKeyword on 16/6/30.
 */
public class MsgFragment extends SwipeBackFragment {
    private static final String ARG_MSG = "arg_msg";
    @Bind(R.id.recy)
    RecyclerView recy;
    @Bind(R.id.et_send)
    EditText etSend;
    @Bind(R.id.btn_send)
    Button btnSend;

    private Chat mChat;
    private MsgAdapter mAdapter;

    public static MsgFragment newInstance(Chat msg) {

        Bundle args = new Bundle();
        args.putParcelable(ARG_MSG, msg);
        MsgFragment fragment = new MsgFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mChat = getArguments().getParcelable(ARG_MSG);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.wechat_fragment_tab_first_msg, container, false);
        ButterKnife.bind(this,view);
        return attachToSwipeBack(view);
    }

    @Override
    protected void onEnterAnimationEnd(Bundle savedInstanceState) {
        super.onEnterAnimationEnd(savedInstanceState);
        // 入场动画结束后执行  优化,防动画卡顿

        _mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        recy.setLayoutManager(new LinearLayoutManager(_mActivity));
        recy.setHasFixedSize(true);
        mAdapter = new MsgAdapter(_mActivity);
        recy.setAdapter(mAdapter);

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = etSend.getText().toString().trim();
                if (TextUtils.isEmpty(str)) return;

                mAdapter.addMsg(new Msg(str));
                etSend.setText("");
                recy.scrollToPosition(mAdapter.getItemCount() - 1);
            }
        });

        mAdapter.addMsg(new Msg(mChat.message));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        recy.setAdapter(null);
        _mActivity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        hideSoftInput();
        ButterKnife.unbind(this);
    }
}
